from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver.common.appiumby import AppiumBy

class CartPage:
    def __init__(self, driver):
        self.driver = driver
        # 定位器
        if driver.capabilities.get('platformName')=='Android':
              self.shopcart_button = (AppiumBy.XPATH, "//android.widget.TextView[@text='购物车']")
              self.product_price = (AppiumBy.XPATH, "//android.webkit.WebView[@text='pages/cart/cart[2]']/android.view.View[2]")
              self.product = (AppiumBy.XPATH, "//android.webkit.WebView[@text='pages/cart/cart[2]']/android.view.View[2]")
              self.add_to_cart_button = (AppiumBy.ANDROID_UIAUTOMATOR, "new UiSelector().text(\"加入购物车\")")
              self.back_button = (AppiumBy.ANDROID_UIAUTOMATOR, "new UiSelector().text(\"\")")
              self.number_cart = (AppiumBy.ANDROID_UIAUTOMATOR, "new UiSelector().text(\"\")")
              self.number_add = (AppiumBy.ANDROID_UIAUTOMATOR, "new UiSelector().text(\"\")")
              self.number_reduce = (AppiumBy.ANDROID_UIAUTOMATOR, "new UiSelector().text(\"\")")
              self.amount = (AppiumBy.ANDROID_UIAUTOMATOR, "new UiSelector().text(\"\")")
              self.to_pay_button = (AppiumBy.XPATH, "//android.widget.TextView[@text='去结算']")
              self.to_buy_button = (AppiumBy.XPATH, "//android.widget.TextView[@text='去结算']")
              self.remove_item_button = (AppiumBy.ANDROID_UIAUTOMATOR, "new UiSelector().text(\"\")")
        elif driver.capabilities.get('platformName')=='ios':
              self.shopcart_button = (AppiumBy.XPATH, "//XCUIElementTypeStaticText[@name='购物车']")
              self.tologin_button = (AppiumBy.XPATH, "//XCUIElementTypeStaticText[contains(@value, '去登陆>')]")
              self.login_button = (AppiumBy.XPATH, "//XCUIElementTypeStaticText[@name='登录']")
        elif driver.capabilities.get('platformName')=='other' :
              print("其他平台")

    # 获取元素
    def wait_element_visible(self, locator, timeout=10):
        element = WebDriverWait(self.driver, timeout).until(EC.visibility_of_element_located(locator))
        return element
    def wait_all_element_visible(self, locator, timeout=10):
        element = WebDriverWait(self.driver, timeout).until(EC.visibility_of_all_elements_located(locator))
        return element
    
    #导航
    def navigate(self):
        shopcart_button = self.wait_element_visible(self.shopcart_button)
        shopcart_button.click()
    
    #返回
    def back(self):
        back_button = self.wait_element_visible(self.back_button)
        back_button.click()

    # 添加购物车
    def add_item_to_cart(self):
        product_1 = self.wait_element_visible(self.product_1)
        product_1.click()
        add_to_cart_button = self.wait_element_visible(self.add_to_cart_button)
        add_to_cart_button.click()

    # # 勾选购物车商品
    # def check_cart_icon(self):
    #     return cart_icon.is_displayed()
    
    
    # def verify_item_in_cart(self, item_name):
    #     return item_name in item_in_cart.text

    # def remove_item_from_cart(self):
    #     remove_from_cart_button = WebDriverWait(self.driver, 10).until(
    #         EC.element_to_be_clickable(self.remove_from_cart_button_locator)
    #     )
    #     remove_from_cart_button.click()

    # def update_quantity(self, quantity):
    #     update_quantity_input = WebDriverWait(self.driver, 10).until(
    #         EC.presence_of_element_located(self.update_quantity_input_locator)
    #     )
    #     update_quantity_input.clear()
    #     update_quantity_input.send_keys(quantity)

    # def click_update_cart_button(self):
    #     update_cart_button = WebDriverWait(self.driver, 10).until(
    #         EC.element_to_be_clickable(self.update_cart_button_locator)
    #     )
    #     update_cart_button.click()

    # def verify_cart_is_empty(self):
    #     empty_cart_message = WebDriverWait(self.driver, 10).until(
    #         EC.presence_of_element_located(self.empty_cart_message_locator)
    #     )
    #     return empty_cart_message.is_displayed()

    # def proceed_to_checkout(self):
    #     proceed_to_checkout_button = WebDriverWait(self.driver, 10).until(
    #         EC.element_to_be_clickable(self.proceed_to_checkout_button_locator)
    #     )
    #     proceed_to_checkout_button.click()

    # def get_total_price(self):
    #     total_price = WebDriverWait(self.driver, 10).until(
    #         EC.presence_of_element_located(self.total_price_locator)
    #     )
    #     return total_price.text

    # def apply_promo_code(self, promo_code):
    #     promo_code_input = WebDriverWait(self.driver, 10).until(
    #         EC.presence_of_element_located(self.promo_code_input_locator)
    #     )
    #     promo_code_input.send_keys(promo_code)
    #     apply_promo_code_button = WebDriverWait(self.driver, 10).until(
    #         EC.element_to_be_clickable(self.apply_promo_code_button_locator)
    #     )
    #     apply_promo_code_button.click()

    # def get_discounted_price(self):
    #     discounted_price = WebDriverWait(self.driver, 10).until(
    #         EC.presence_of_element_located(self.discounted_price_locator)
    #     )
    #     return discounted_price.text